package com.kzj.laimeimei.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.kzj.laimeimei.R;
import com.kzj.laimeimei.beans.BookDateTime;
import com.kzj.laimeimei.beans.ServiceBookBean;
import com.kzj.laimeimei.beans.TimeBean;
import com.kzj.laimeimei.beans.TimeListBean;
import com.kzj.laimeimei.global.App;
import com.kzj.laimeimei.global.Constant;
import com.kzj.laimeimei.recyclerCard.basic.MaterialListView;
import com.kzj.laimeimei.recyclerCard.card.other.ChooseLeftTimeCard;
import com.kzj.laimeimei.recyclerCard.card.other.ChooseRightTimeCard;
import com.kzj.laimeimei.request.BaseResponse;
import com.kzj.laimeimei.request.ICallBackForRequestServer;
import com.kzj.laimeimei.request.RequestServerManager;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by xyg on 2015/11/5.
 */
public class ChooseTimeActivity extends GBaseActivity implements View.OnClickListener {

    private MaterialListView material_listview_date;
    private MaterialListView material_listview_time;
    public static String TagDateList="TagDateList";
    public static String TagTimeList="TagTimeList";
    public String sid;
    public List<ServiceBookBean.ServiceTimeEntity> timeList;
    public List<String> dataList;
    private TextView textView_finish;
    private TextView textView_cancel;
    private BookDateTime bookDateTime;
    public static String bookDateTimeTag="bookDateTimeTag";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentViewFor720_1280(R.layout.activity_choose_time);
        findViewByIds();
        initDatas();
        fillArrayInListView();
        TaskGetTimeList(App.getInstance().getLongitude(), App.getInstance().getLatitude(), sid,App.getInstance().getCity(),bookDateTime.getBooking_date());
    }

    @Override
    protected void findViewByIds() {
        material_listview_date= (MaterialListView) findViewById(R.id.material_listview_left);
        material_listview_time= (MaterialListView) findViewById(R.id.material_listview_right);
        textView_finish= (TextView) findViewById(R.id.textView_finish);textView_finish.setOnClickListener(this);
        textView_cancel= (TextView) findViewById(R.id.textView_cancel);textView_cancel.setOnClickListener(this);
    }

    @Override
    protected void initDatas() {
        setTopTitle("预约时间");
        setLeftImg(View.VISIBLE, R.drawable.back);
        sid=getIntent().getStringExtra(ServiceOrderActivity.TagSID);
        dataList= (List<String>) getIntent().getSerializableExtra(TagDateList);
        timeList= (List<ServiceBookBean.ServiceTimeEntity>) getIntent().getSerializableExtra(TagTimeList);
        bookDateTime= (BookDateTime) getIntent().getSerializableExtra(bookDateTimeTag);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void fillArrayInListView() {
        for (int i=0;i<dataList.size();i++) {
            ChooseLeftTimeCard chooseLeftTimeCard = new ChooseLeftTimeCard(this);
            TimeBean timeBean = new TimeBean();
            timeBean.setTime(dataList.get(i));
            chooseLeftTimeCard.setTimeBean(timeBean);
            chooseLeftTimeCard.setSid(sid);
            chooseLeftTimeCard.setBookDateTime(bookDateTime);
            chooseLeftTimeCard.setMaterialListViewDate(material_listview_date);
            chooseLeftTimeCard.setMaterialListViewTime(material_listview_time);
            if (i==0){
                chooseLeftTimeCard.setIsSelected(true);
            }
            material_listview_date.add(chooseLeftTimeCard);
        }
        for (int i=0;i<timeList.size();i++) {
            ChooseRightTimeCard chooseRightTimeCard = new ChooseRightTimeCard(this);
            TimeBean timeBean = new TimeBean();
            timeBean.setTime(timeList.get(i).getTime());
            chooseRightTimeCard.setTimeBean(timeBean);
            chooseRightTimeCard.setBookDateTime(bookDateTime);
            chooseRightTimeCard.setMaterialListView(material_listview_time);
            if (i==0){
                chooseRightTimeCard.setIsSelected(true);
            }
            material_listview_time.add(chooseRightTimeCard);
        }
    }

    public void onEventMainThread(BookDateTime bookDateTime) {
      this.bookDateTime=bookDateTime;
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.textView_finish:
                if (bookDateTime!=null && bookDateTime.getBooking_time()!=null) {
                    finish();
                    EventBus.getDefault().post(bookDateTime);
                }else
                {
                    App.showToast("请选择一个时间段");
                }
                break;
            case R.id.textView_cancel:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void TaskGetTimeList(String lng,String lat,String sid,String city, final String date) {
        RequestServerManager.getInstance().handleMethod(this, material_listview_date, null, false,
                Constant.getRootUrl() + Constant.Action.Action_Order_Choose_Time, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_Order_Choose_Time, lng, lat, sid, city, date), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        material_listview_time.clear();
                        TimeListBean timeListBean = JSON.parseObject(JSON.toJSONString(result), TimeListBean.class);
                        BookDateTime bookDateTimeInit = null;
                        if (bookDateTime == null) {
                            bookDateTime = new BookDateTime();
                            bookDateTime.setBooking_date(date);
                            bookDateTime.setBooking_time(timeListBean.getRetval().get(0).getTime());
                            EventBus.getDefault().post(bookDateTime);
                        }
                        for (int i = 0; i < timeListBean.getRetval().size(); i++) {
                            ChooseRightTimeCard chooseRightTimeCard = new ChooseRightTimeCard(ChooseTimeActivity.this);
                            TimeBean timeBean = new TimeBean();
                            timeBean.setTime(timeListBean.getRetval().get(i).getTime());
                            chooseRightTimeCard.setTimeBean(timeBean);
                            chooseRightTimeCard.setServiceTimeEntity(timeListBean.getRetval().get(i));
                            chooseRightTimeCard.setBookDateTime(bookDateTime);
                            chooseRightTimeCard.setMaterialListView(material_listview_time);

                            if (i == 0) {
                                chooseRightTimeCard.setIsSelected(true);
                            }
                            material_listview_time.add(chooseRightTimeCard);
                        }
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName()); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
        MobclickAgent.onResume(this);          //统计时长
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName()); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息
        MobclickAgent.onPause(this);
    }
}
